
import Box1 from "./Box1";
import Box2 from "./Box2";
import Box3 from "./Box3";
/**
 * 游戏控制脚本。定义了几个Box，createBoxInterval等变量，能够在IDE显示及设置该变量
 * 更多类型定义，请参考官方文档
 */
export default class GameControl extends Laya.Script {
    /** @prop {name:box1,tips:"掉落容器预制体对象1",type:Prefab}*/
    box1: Laya.Prefab;
    private box_1: Laya.Sprite;
    /** @prop {name:box2,tips:"掉落容器预制体对象2",type:Prefab}*/
    box2: Laya.Prefab;
    private box_2: Laya.Sprite;
    /** @prop {name:box3,tips:"掉落容器预制体对象3",type:Prefab}*/
    box3: Laya.Prefab;
    private box_3: Laya.Sprite;
    /**是否已经开始游戏 */
    private _started: boolean = false;
    /**子弹和盒子所在的容器对象 */
    private _gameBox: Laya.Sprite;

    /**盒子等级 */
	private _level: number;

    private _hasBox1:boolean;
    private _hasBox2:boolean;
    private _hasBox3:boolean;

    /* 构造函数 */
    constructor() { 
        super(); 
    }
    
    /* 
    1.初始化时加载一个随机球球
    2.按下鼠标移动获得X轴，这个球球跟着移动
    3.鼠标抬起，球球下落
    4.球球下落碰到地板，再次加载一个随机球球
    */
    onEnable(): void {
        this._gameBox = this.owner.getChildByName("gameBox") as Laya.Sprite;
        // this.createBox();
    }

    // onUpdate(): void {

    //     /* 判断舞台盒子上有没有球球 */
    //     this._hasBox1= this._gameBox.getChildByName("box1") == null ? false : true;

    //     this._hasBox2 = this._gameBox.getChildByName("box2") == null ? false : true;

    //     this._hasBox3 = this._gameBox.getChildByName("box3") == null ? false : true;

    //     // console.log("hasBox1:"+this._hasBox1+"---hasBox2:"+this._hasBox2+"---hasBox3:"+this._hasBox3);
    // }

    createBox(): void {
        /* 盒子等级：1/2/3 随机掉落 */
        this._level = Math.round(Math.random() * 2) + 1;

         //使用对象池创建盒子
        switch(this._level){
            case 1 :
                this.box_1 = Laya.Pool.getItemByCreateFun("box1", this.box1.create, this.box1);
                this.box_1.pos(Laya.stage.width/2, 0);
                this._gameBox.addChild(this.box_1);
                break;
            case 2 :
                this.box_2 = Laya.Pool.getItemByCreateFun("box2", this.box2.create, this.box2);
                this.box_2.pos(Laya.stage.width/2, 0);
                this._gameBox.addChild(this.box_2);
                break;
            case 3 :
                this.box_3 = Laya.Pool.getItemByCreateFun("box3", this.box3.create, this.box3);
                this.box_3.pos(Laya.stage.width/2, 0);
                this._gameBox.addChild(this.box_3);
                break;
        }
    }

    /* 鼠标按下 */
    onMouseDown(e: Laya.Event): void {
        console.log("鼠标按下");
        // Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
        
        this.createBox();
    }

    // /* 鼠标抬起 */
    // onMouseUp(e: Laya.Event): void {
    //     console.log("鼠标抬起");
    //     Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.mouseMove);

    //     if(this._hasBox1){
    //         let _rig:Laya.RigidBody;
    //         _rig = this.box_1.getComponent(Laya.RigidBody);
    //         _rig.type = "dynamic";
    //     }

    //     if(this._hasBox2){
    //         let _rig:Laya.RigidBody;
    //         _rig = this.box_2.getComponent(Laya.RigidBody);
    //         _rig.type = "dynamic";
    //     }

    //     if(this._hasBox3){
    //         let _rig:Laya.RigidBody;
    //         _rig = this.box_3.getComponent(Laya.RigidBody);
    //         _rig.type = "dynamic";
    //     }
    // }

    // /* 鼠标移动 */
    // mouseMove(e: Laya.Event): void {
    //     let mouseX: number = e.stageX;
    //     let currentBox : number;

    //     if(this._hasBox1){
    //         currentBox = 1;
    //     }else if(this._hasBox2){
    //         currentBox = 2;
    //     }else if(this._hasBox3){
    //         currentBox = 3;
    //     }

    //     switch(currentBox){
    //         case(1):
    //             this.box_1.x = mouseX; 
    //             break;
    //         case(2):
    //             this.box_2.x = mouseX; 
    //             break;
    //         case(3):
    //             this.box_3.x = mouseX; 
    //             break;
    //     }

    //     // if(this._hasBox1){
    //     //     this.box_1.x = mouseX; 
    //     // }
    //     // if(this._hasBox2){
    //     //     this.box_2.x = mouseX; 
    //     // }
    //     // if(this._hasBox3){
    //     //     this.box_3.x = mouseX; 
    //     // }

    //     console.log("mouseMove方法中当前控制的盒子："+currentBox);
    // }

    // onTriggerEnter(other: any, self: any, contact: any): void {
        
    // }

    /**开始游戏，通过激活本脚本方式开始游戏*/
    startGame(): void {
        if (!this._started) {
            this._started = true;
            this.enabled = true;
        }
    }

    /**结束游戏，通过非激活本脚本停止游戏 */
    stopGame(): void {
        this._started = false;
        this.enabled = false;
        this._gameBox.removeChildren();
    }
}